package com.pptv.mtbu.ottflow.action.authority;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.*;

import com.opensymphony.xwork2.interceptor.*;

import com.pptv.mtbu.common.exception.MtbuException;
import com.pptv.mtbu.ottflow.action.WebConstant;


public class SuperAdminAuthorityInterceptor
	extends AbstractInterceptor
{
    /**
     * 
     */
    private static final long serialVersionUID = 312283475784663373L;
    static final Log logger = LogFactory.getLog(SuperAdminAuthorityInterceptor.class);
    
	public String intercept(ActionInvocation invocation)
		throws Exception
	{

		ActionContext ctx = ActionContext.getContext();
		

		String role = (String)ctx.getSession()
			.get(WebConstant.ROLE);
		
		String [] pageIdx = null;
		try{
		    pageIdx = (String[]) ctx.getParameters().get("pageIdx");

		    /*
		    Map map = ctx.getParameters();
		    Set<String> key = map.keySet();
		    for (Iterator it = key.iterator(); it.hasNext();) {
	            String s = (String) it.next();
	            logger.info(map.get(s));
	        }
	        */
		    
		    if(pageIdx!=null) Integer.parseInt(pageIdx[0],10);
		}
		catch( NumberFormatException e){
		    pageIdx[0] = "0";
		    
		    Map map = ctx.getParameters();
            Set<String> key = map.keySet();
            for (Iterator it = key.iterator(); it.hasNext();) {
                String s = (String) it.next();
                Object obj = map.get(s);
                String [] tmp = (String[]) obj;
                logger.info(tmp[0]);
            }
            
        }
		catch( Exception e){
	
		    MtbuException me = new MtbuException(e.getMessage());
		    me.setStackTrace(e.getStackTrace());
		    
		    throw me;
		}
		
	
		if (role != null
			&& role.equals(WebConstant.SUPERADMIN_ROLE))
		{
			return invocation.invoke();
		}
		else
		{
			return Action.LOGIN;
		}
	}
}